package zy.viewpagerslidingconflict;

import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager vp_show;
    private LinearLayout wel_ll;
    private FragmentPagerAdapter adapter;
    private List<Fragment> fragments;
    private ImageView wel_arrow;
    private ImageView wel_start;
    private boolean isFromFloatWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);//隐藏导航栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome_aty);
        initViews();
    }
    protected void initViews() {
        vp_show = (ViewPager) findViewById(R.id.vp_show);
        wel_ll = (LinearLayout) findViewById(R.id.wel_ll);
        wel_arrow = (ImageView) findViewById(R.id.wel_arrow);
        wel_start = (ImageView) findViewById(R.id.wel_start);
        fragments = new ArrayList<>();
        fragments.add(new FragmentExd1());
        fragments.add(new FragmentExd2());
        fragments.add(new FragmentExd3());

        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };

        vp_show.setAdapter(adapter);

        vp_show.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 2) {
                    wel_start.setVisibility(View.VISIBLE);
                    wel_arrow.setVisibility(View.GONE);
                } else {
                    wel_start.setVisibility(View.GONE);
                    wel_arrow.setVisibility(View.VISIBLE);
                }
                setPointColor(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        wel_arrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vp_show.setCurrentItem(vp_show.getCurrentItem() + 1);
            }
        });
        wel_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isFromFloatWindow) {
//                    if (getXPenDetectState()) {
//                        sendBroadcast(new Intent(BroadcastMessageSpace.OPEN_FLOAT_WINDOW));
//                        finishout();
//                    } else {
//                        Intent intent = new Intent(WelcomeAty.this, HistoryAty.class);
//                        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//                        startActivity(intent);
//                        finish();
//                    }
                } else {
//                    Intent intent = new Intent(WelcomeAty.this, HistoryAty.class);
//                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//                    startActivity(intent);
//                    finish();
//                    sendBroadcast(new Intent(BroadcastMessageSpace.OPEN_FLOAT_BALL));
                }
            }
        });
    }



    private boolean getXPenDetectState() {
        try {
            if (Settings.System.getInt(getContentResolver(), "xpen_switch") == 1) {
                return true;
            }
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void setPointColor(int position) {
        for (int i = 0; i < 3; i++) {
            if (i == position) {
                wel_ll.getChildAt(i).setBackgroundResource(R.drawable.welcome_circle1);
            } else {
                wel_ll.getChildAt(i).setBackgroundResource(R.drawable.welcome_circle2);
            }
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        hideBottomUIMenu();
    }
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    @Override
    protected void onDestroy() {
        adapter = null;
        fragments.clear();
        fragments = null;
        wel_arrow = null;
        super.onDestroy();
    }
}
